package com.why.service.Iml;


import com.alibaba.fastjson.JSONObject;
import com.netflix.client.ClientException;
import com.why.service.MsmService;
import com.why.yygh.vo.msm.MsmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @program: guli_parent
 * @description:
 * @author: @why
 * @create: 2021-02-15 17:14
 **/
@Service
public class MsmServiceImpl implements MsmService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    JavaMailSenderImpl sender;

    /**
     * 发送邮箱验证码
     *
     * @return
     */
    @Override
    public boolean sendSMS(String code, String emial) {
        String s = redisTemplate.opsForValue().get(emial);
        if(s==null){

            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("申请注册的验证码为...");
            code = "【" + code + "】";
            message.setText("申请注册的验证码为" + code + ",5分钟内有效!");
            System.out.println(code);
            message.setFrom("1585049082@qq.com");
            message.setTo(emial);
            sender.send(message);
            redisTemplate.opsForValue().set(emial,code);
            return  true;
        }
        return true;

    }

    @Override
    public boolean sendSMS(MsmVo msmVo) {
        if (msmVo.getPhone() != null) {
//mq只发741886
            return this.sendSMS("741886", "1915356072@qq.com");


        }


        return false;
    }
}
